#ifndef __PLATFORM_LIB_JSON_H__
#define __PLATFORM_LIB_JSON_H__

#ifdef __cplusplus
extern "C" 
{
#endif

#include "pf_common.h"

/******************************************************************************************************/

#define PF_JSON_PACKAGE_STRING "JSON C Library 0.2"

#define PF_JSON_PACKAGE_BUGREPORT "michael@metaparadigm.com"

#define PF_JSON_PACKAGE_NAME "JSON C Library"

#define PF_JSON_PACKAGE_TARNAME "json-c"

#define PF_JSON_PACKAGE_VERSION "0.2"

#ifdef DEBUG
#define PF_JSON_SET_DEBUG(x) pf_json_set_debug(x)
#define PF_JSON_GET_DEBUG() pf_json_get_debug()
#else
#define PF_JSON_SET_DEBUG(x) if(0){pf_json_set_debug(x)}
#define PF_JSON_GET_DEBUG() (0)
#endif

/* forward structure definitions */
typedef int boolean;

typedef struct pf_json_object pf_json_object_t;

typedef struct pf_json_tokener pf_json_tokener_t;

/* supported object types */
typedef enum pf_json_type 
{
    /* If you change this, be sure to update json_type_to_name() too */
    PF_JSON_TYPE_NULL,
    PF_JSON_TYPE_BOOLEAN,
    PF_JSON_TYPE_DOUBLE,
    PF_JSON_TYPE_INT,
    PF_JSON_TYPE_OBJECT,
    PF_JSON_TYPE_ARRAY,
    PF_JSON_TYPE_STRING,
} pf_json_type_e;

typedef enum pf_json_tokener_error 
{
    PF_JSON_TOK_SUCCESS,
    PF_JSON_TOK_CONTINUE,
    PF_JSON_TOK_ERR_DEPTH,
    PF_JSON_TOK_ERR_PARSE_EOF,
    PF_JSON_TOK_ERR_PARSE_UNEXPECTED,
    PF_JSON_TOK_ERR_PARSE_NULL,
    PF_JSON_TOK_ERR_PARSE_BOOLEAN,
    PF_JSON_TOK_ERR_PARSE_NUMBER,
    PF_JSON_TOK_ERR_PARSE_ARRAY,
    PF_JSON_TOK_ERR_PARSE_OBJECT_KEY_NAME,
    PF_JSON_TOK_ERR_PARSE_OBJECT_KEY_SEP,
    PF_JSON_TOK_ERR_PARSE_OBJECT_VALUE_SEP,
    PF_JSON_TOK_ERR_PARSE_STRING,
    PF_JSON_TOK_ERR_PARSE_COMMENT
} pf_json_tokener_error_e;

/*********************************************************************************************
Function Name:  pf_json_set_debug
Description  :  Set this to debug some info related,set 1 and define DEBUG will print debug info 
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_set_debug(int debug);

/*********************************************************************************************
Function Name:  pf_json_get_debug
Description  :  Get the debug flag 
Inputs       :                          :   
Outputs      :  return value            :  the debug flag   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_get_debug(void);

/* reference counting functions */

/*********************************************************************************************
Function Name:  pf_json_get_obj
Description  :  Get the json object and the reference will plus 1
Inputs       :                          :   
Outputs      :  return value            :  the pointer of the json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_get_obj(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_put_obj
Description  :  Return the json object and the reference will minus 1, when the reference count
                becomes 0, then free the object
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_put_obj(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_is_obj_type
Description  :  Judge the json object 's type
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_is_obj_type(pf_json_object_t* obj, pf_json_type_e type);

/*********************************************************************************************
Function Name:  pf_json_get_obj_type
Description  :  Get the json object 's type
Inputs       :                          :   
Outputs      :  return value            :  the type of the json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_type_e pf_json_get_obj_type(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_obj_to_json_str
Description  :  Convert the json object to json string
Inputs       :                          :   
Outputs      :  return value            :  the json string 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern const char* pf_json_obj_to_json_str(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_create_obj
Description  :  Init a new json object
Inputs       :                          :   
Outputs      :  return value            :  the type of the json object  
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_obj(void);

/*********************************************************************************************
Function Name:  pf_json_add_sub_obj
Description  :  Add the key, value under the json object
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_add_sub_obj(pf_json_object_t* obj, 
                                      const char* key, 
                                      pf_json_object_t* val);

/*********************************************************************************************
Function Name:  pf_json_get_sub_obj
Description  :  Get the json object by the key under current json object
Inputs       :                          :   
Outputs      :  return value            :  the pointer of the json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_get_sub_obj(pf_json_object_t* obj, const char* key);

/*********************************************************************************************
Function Name:  pf_json_del_sub_obj
Description  :  Delete the json object by the key under current json object
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_del_sub_obj(pf_json_object_t* obj, const char* key);

/*********************************************************************************************
Function Name:  pf_json_obj_for_each
Description  :  Iterate through all keys and values of an object
Inputs       :                          :   
Outputs      :  return value            :  cb fail or success 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
int pf_json_obj_for_each(pf_json_object_t* obj, 
                         void* arg, 
                         int (*cb)(void* arg, char* key, pf_json_object_t* val));

/*********************************************************************************************
Function Name:  pf_json_create_array_obj
Description  :  Init a new array json object
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_array_obj(void);

/*********************************************************************************************
Function Name:  pf_json_array_obj_len
Description  :  Get the array json object 's length
Inputs       :                          :   
Outputs      :  return value            :  the length of the array 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_array_obj_len(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_array_obj_add_item
Description  :  Add new json object under the current array object
Inputs       :                          :   
Outputs      :  return value            :   0 suucess, other fail
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_array_obj_add_item(pf_json_object_t* obj, pf_json_object_t* val);

/*********************************************************************************************
Function Name:  pf_json_array_obj_put_item
Description  :  Set the val to the index of current array object, if index exist, then replace
                the json object , if the index is larger then the array length, the array will
                expand
Inputs       :                          :   
Outputs      :  return value            :  0 suucess, other fail 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_array_obj_put_item(pf_json_object_t* obj, 
                                        int idx, 
                                        pf_json_object_t* val);

/*********************************************************************************************
Function Name:  pf_json_array_obj_get_item
Description  :  Get the json object by the index under the current array json object
Inputs       :                          :   
Outputs      :  return value            :  the pointer of json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_array_obj_get_item(pf_json_object_t* obj, int idx);

/*********************************************************************************************
Function Name:  pf_json_create_bool_obj
Description  :  Init a new boolean json object
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_bool_obj(boolean b);

/*********************************************************************************************
Function Name:  pf_json_get_bool
Description  :  Get the boolean value from the boolean json object
Inputs       :                          :   
Outputs      :  return value            :   the boolean value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern boolean pf_json_get_bool(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_create_int_obj
Description  :  Init a new int json object
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_int_obj(int32_t i);

/*********************************************************************************************
Function Name:  pf_json_create_int64_obj
Description  :  Init a new int64 json object
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_int64_obj(int64_t i);

/*********************************************************************************************
Function Name:  pf_json_get_int
Description  :  Get the int value from the int json object
Inputs       :                          :   
Outputs      :  return value            :  the int value 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int32_t pf_json_get_int(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_get_int64
Description  :  Get the int64 value from the int64 json object
Inputs       :                          :   
Outputs      :  return value            :   the int64 value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int64_t pf_json_get_int64(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_create_double_obj
Description  :  Init a new double json object
Inputs       :                          :   
Outputs      :  return value            :  the pointer of json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_double_obj(double d);

/*********************************************************************************************
Function Name:  pf_json_get_double
Description  :  Get the double value from the double json object
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern double pf_json_get_double(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_create_str_obj
Description  :  Init a new string json object
Inputs       :                          :   
Outputs      :  return value            :   the double value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_str_obj(const char* s);

/*********************************************************************************************
Function Name:  pf_json_create_str_len_obj
Description  :  Init a new string json object of defind length
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_create_str_len_obj(const char* s, int len);

/*********************************************************************************************
Function Name:  pf_json_get_str
Description  :  Get the string value from the json object
Inputs       :                          :   
Outputs      :  return value            :  the pointer of json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern const char* pf_json_get_str(pf_json_object_t* obj);

/*********************************************************************************************
Function Name: pf_json_get_str_len 
Description  : Get the string length from the json object 
Inputs       :                          :   
Outputs      :  return value            :   the string value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_get_str_len(pf_json_object_t* obj);

/*********************************************************************************************
Function Name:  pf_json_create_tokener
Description  :  Init a new json tokener
Inputs       :                          :   
Outputs      :  return value            :   the pointer of json tokener
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_tokener_t* pf_json_create_tokener(void);

/*********************************************************************************************
Function Name:  pf_json_destroy_tokener
Description  :  Free the current json tokener 
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_destroy_tokener(pf_json_tokener_t* tok);

/*********************************************************************************************
Function Name:  pf_json_reset_tokener
Description  :  Reset the current json tokener
Inputs       :                          :   
Outputs      :  return value            :   
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern void pf_json_reset_tokener(pf_json_tokener_t* tok);

/*********************************************************************************************
Function Name:  pf_json_parse
Description  :  Parse the string to json object
Inputs       :                          :   
Outputs      :  return value            :  the pointer of the json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_parse(const char* str);

/*********************************************************************************************
Function Name:  pf_json_parse_verbose
Description  :  Parse the string to json object and get the error type
Inputs       :                          :   
Outputs      :  return value            :  the pointer of the json object 
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_parse_verbose(const char* str, pf_json_tokener_error_e* error);

/*********************************************************************************************
Function Name:  pf_json_parse_ex
Description  :  Parse the string to json object ,you can use this function many times until get
                what you want
Inputs       :                          :   
Outputs      :  return value            :   the pointer of the json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern pf_json_object_t* pf_json_parse_ex(pf_json_tokener_t* tok, const char* str, int len);

/*********************************************************************************************
Function Name:  pf_json_create_obj_from_file
Description  :  Get the json object from file
Inputs       :                          :   
Outputs      :  return value            :   the pointer of the json object
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern struct pf_json_object* pf_json_create_obj_from_file(const char* filename);

/*********************************************************************************************
Function Name:  pf_json_obj_to_file
Description  :  Convert the current json object to fiel
Inputs       :                          :   
Outputs      :  return value            :   0 success, other fail
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern int pf_json_obj_to_file(char* filename, struct pf_json_object* obj);

/*********************************************************************************************
Function Name:  pf_json_obj_type_to_name
Description  :  Get the name of the json type
Inputs       :                          :   
Outputs      :  return value            :   the name of the json type
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-08-24                    create
**********************************************************************************************/
extern const char* pf_json_obj_type_to_name(enum pf_json_type o_type); 

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /* #ifndef __PLATFORM_LIB_JSON_H__ */
